package com.scap.authen.controller;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;
import com.scap.authen.service.UserLoginService;
import com.scap.authen.util.DataTable;


@Controller
@RequestMapping(value = "/user")
public class EditUserController {

	@Autowired
	private UserLoginService voUserLoginService;
	@Autowired
	private ApplicationContext context;
	DataTable voDataTable = new DataTable();

	@RequestMapping(value = "/edit",method = RequestMethod.GET)
	public ModelAndView application(HttpServletRequest request) {
		Locale voLocale = RequestContextUtils.getLocale(request);
		ModelAndView voModelAndView = new ModelAndView("login/editUser");
		voModelAndView.addObject("page", "Edit User");
		voModelAndView.addObject("status", request.getParameter("status"));
		voModelAndView.addObject("message", request.getParameter("message")!=null?request.getParameter("message").replace("_Name_", context.getMessage("class.UserLogin", null, voLocale)):"");
		voModelAndView.addObject("oLanguage", voDataTable.getDataTableLanguage(context,request));
		return voModelAndView;
	}

	@RequestMapping(value = "/userDataTables", method = RequestMethod.GET, 
			produces = "application/json; charset=utf-8")
	public @ResponseBody String springPaginationDataTables(
			HttpServletRequest request) throws IOException {
		String vaUserId = request.getParameter("vaUserId");
		String vaName = request.getParameter("vaName");

		List<Object> userList = null;
		if(vaUserId==null && vaName==null){
			userList = voUserLoginService.getUserLoginLastUpdate();
		}else{
			userList = voUserLoginService.getAllUserLogin();
			userList = voUserLoginService.getListBasedOnSearchParameter(
					vaUserId,vaName, userList);
		}

		return voDataTable.springPaginationDataTables(request,userList);
	}


}
